package com.neu.questionnaireplus.service.impl;

import com.neu.questionnaireplus.dao.AnswererDao;
import com.neu.questionnaireplus.dao.GroupDao;
import com.neu.questionnaireplus.dao.GroupToAnswererDao;
import com.neu.questionnaireplus.entity.Answerer;
import com.neu.questionnaireplus.entity.Group;
import com.neu.questionnaireplus.entity.GroupToAnswerer;
import com.neu.questionnaireplus.service.AnswererService;
import com.neu.questionnaireplus.utils.FileToAnswerers;
import com.neu.questionnaireplus.utils.common.CommonUtils;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile;

@Validated
@Service
public class AnswererServiceImpl implements AnswererService {

  @Autowired
  private GroupDao groupDao;
  @Autowired
  private AnswererDao answererDao;

  @Autowired
  private GroupToAnswererDao groupToAnswererDao;

  @Override
  public Answerer queryAnswererByUsername(String username) {
    return answererDao.queryAnswererByUsername(username);
  }

  @Override
  public List<Answerer> queryAnswerer() {
    return answererDao.queryAnswerer();
  }

  @Override
  public List<Answerer> queryExistAnswerer() {
    return answererDao.queryExistAnswerer();
  }

  @Override
  public List<Answerer> selectAnswererList(Answerer answerer) {
    return answererDao.selectAnswererList(answerer);
  }

  @Override
  public Answerer queryAnswererByID(String id) {
    return answererDao.queryAnswererByID(id);
  }

  @Transactional
  @Override
  public boolean insertAnswerer(Answerer answerer) {
    if (answerer != null && !answerer.getId().equals("")) {
      try {
        String username = answerer.getUsername();
        String telnumber = answerer.getTelnumber();
        if (answererDao.queryAnswererByPhone(telnumber) != null) {
          throw new RuntimeException("答者电话已存在");
        } else if (answererDao.queryAnswererByUsername(username) != null) {
          throw new RuntimeException("用户名已存在");
        } else {
          int i = answererDao.insertAnswerer(answerer);
          if (i == 1) {
            return true;
          } else {
            throw new RuntimeException("插入答者失败！！" + answerer);
          }
        }
      } catch (Exception e) {
        throw new RuntimeException("插入答者失败！！" + e.getMessage());
      }
    } else {
      throw new RuntimeException("插入答者失败,答者id不能为空");
    }
  }

  @Transactional
  @Override
  public boolean updateAnswerer(Answerer answerer) {
    if (answerer != null && !answerer.getId().equals("")) {
      try {
        String username = answerer.getUsername();
        String telnumber = answerer.getTelnumber();
        Answerer answerer1 = answererDao.queryAnswererByPhone(telnumber);
        Answerer answerer2 = answererDao.queryAnswererByUsername(username);
        if ((answerer1 != null || answerer2 != null) && (
            (answerer1 != null && !answerer.getId().equals(answerer1.getId())) || (answerer2 != null
                && !answerer.getId().equals(answerer2.getId())))) {
          throw new RuntimeException("修改的用户名或手机号与其他用户有冲突");
        } else {
          int i = answererDao.updateAnswerer(answerer);
          if (i == 1) {
            return true;
          } else {
            throw new RuntimeException("更新答者失败！！" + answerer);
          }
        }
      } catch (Exception e) {
        throw new RuntimeException("更新答者失败！！" + e.getMessage());
      }
    } else {
      throw new RuntimeException("更新答者失败,答者id不能为空");
    }
  }

  @Transactional
  @Override
  public boolean deleteAnswerer(String id) {
    if (id != null && !id.equals("")) {
      try {
        int i = answererDao.deleteAnswerer(id);
        if (i == 1) {
          return true;
        } else {
          throw new RuntimeException("删除答者失败！！" + id);
        }
      } catch (Exception e) {
        throw new RuntimeException("删除答者失败！！" + e.getMessage());
      }
    } else {
      throw new RuntimeException("删除答者失败,答者id不能为空");
    }
  }

  @Override
  public Answerer queryAnswererByPhone(String telnumber) {
    if (telnumber != null && !telnumber.equals("")) {
      Answerer t = null;
      t = answererDao.queryAnswererByPhone(telnumber);
      if (t == null) {
        throw new RuntimeException("未查询到该电话号码的答者");
      } else {
        return t;
      }
    } else {
      throw new RuntimeException("电话号码为空！！");
    }
  }

  @Override
  public boolean loginByPassword(String username, String password) {
    Answerer t = null;
    t = answererDao.queryAnswererByUsername(username);
    if (t == null) {
      throw new RuntimeException("用户名不存在");
    } else {
      if (t.getPassword().equals(password)) {
        if (t.getDelFlag() == 1 || t.getStatus() == 1) {
          throw new RuntimeException("账户被冻结或已经被删除请联系管理员");
        } else {
          t.setLastLoginDate(CommonUtils.getDateByDateString(CommonUtils.getFormatDateTimeNow()));
          boolean res = updateAnswerer(t);
          return res;
        }
      } else {
        throw new RuntimeException("密码错误");
      }
    }
  }

  @Override
  public boolean checkSecurityQuestion(String username, String answer) {
    Answerer t = null;
    t = answererDao.queryAnswererByUsername(username);
    if (t == null) {
      throw new RuntimeException("用户名不存在");
    } else {
      if (t.getSecurityQuestionAnswer().equals(answer)) {
        return true;
      } else {
        throw new RuntimeException("密保验证错误");
      }
    }
  }

  @Override
  public boolean hasDataRelation(String id) {
    //答者在某个群组中是否还有未回答的问卷，如果有则还具有数据关联
    return false;
  }

  @Override
  public List<Answerer> queryAnswererByName(String username) {
    return answererDao.queryAnswererByName(username);
  }

  @Transactional
  @Override
  public boolean forbiddenAnswerer(String id) {
    Answerer t = null;
    t = answererDao.queryAnswererByID(id);
    if (t == null) {
      throw new RuntimeException("答者不存在");
    } else {
      if (t.getStatus() == 1) {//表示已经被冻结
        throw new RuntimeException("答者已经被冻结不可重复冻结");
      } else {
        t.setStatus(1);
        int flag = answererDao.updateAnswerer(t);
        if (flag == 1) {
          return true;
        } else {
          throw new RuntimeException("冻结用户失败");
        }
      }
    }
  }

  @Transactional
  @Override
  public boolean freeAnswerer(String id) {
    Answerer t = null;
    t = answererDao.queryAnswererByID(id);
    if (t == null) {
      throw new RuntimeException("答者不存在");
    } else {
      if (t.getStatus() == 0) {//表示未被冻结
        throw new RuntimeException("答者未处于冻结状态无需解冻答者");
      } else {
        t.setStatus(0);
        int flag = answererDao.updateAnswerer(t);
        if (flag == 1) {
          return true;
        } else {
          throw new RuntimeException("解冻用户失败");
        }
      }
    }
  }

  @Transactional
  @Override
  public boolean modifyAnswererInfo(Answerer answerer) {
    String id = answerer.getId();
    if (answererDao.queryAnswererByID(id) != null) {
      if (hasDataRelation(id)) {
        throw new RuntimeException("该答者正在服务一些群组，不可修改信息");
      } else {
        int flag = answererDao.updateAnswerer(answerer);
        if (flag == 1) {
          return true;
        } else {
          throw new RuntimeException("修改失败");
        }
      }
    } else {
      throw new RuntimeException("答者不存在");
    }
  }

  @Transactional
  @Override
  public boolean deleteAnswererInfo(Answerer answerer) {
    String id = answerer.getId();
    if (answererDao.queryAnswererByID(id) != null) {
      if (hasDataRelation(id)) {
        throw new RuntimeException("该答者正在服务一些群组，不可删除该答者");
      } else {
        answerer.setDelFlag(1);
        int flag = answererDao.updateAnswerer(answerer);
        if (flag == 1) {
          return true;
        } else {
          throw new RuntimeException("删除失败");
        }
      }
    } else {
      throw new RuntimeException("答者不存在");
    }
  }

  @Override
  public List<Answerer> importFileToAnswerers(MultipartFile file) throws RuntimeException{
    List<Answerer> list = FileToAnswerers.importExcel(file);
    for (Answerer answerer : list){
      insertAnswerer(answerer);
    }
    return list;
  }

  @Override
  public boolean loginByPhone(String phone, String inputStr, String realStr) {
    Answerer answerer = answererDao.queryAnswererByPhone(phone);
    if (answerer != null) {
      if (answerer.getStatus() == 1 || answerer.getDelFlag() == 1) {
        throw new RuntimeException("该用户被冻结或被删除");
      } else {
        if (inputStr.equals(realStr)) {
          return true;
        } else {
          throw new RuntimeException("手机验证码不正确");
        }
      }
    } else {
      throw new RuntimeException("该手机号下的租户不存在！");
    }
  }

  @Override
  public boolean checkRegisterStr(String phone, String inputStr, String realStr) {
    Answerer answerer = answererDao.queryAnswererByPhone(phone);
    if (answerer == null) {
      if (inputStr.equals(realStr)) {
        return true;
      } else {
        throw new RuntimeException("手机验证码不正确");
      }
    } else {
      throw new RuntimeException("已经存在该租户，同一个手机号不能重复注册");
    }
  }

  @Override
  public boolean checkResetPwdStr(String phone, String inputStr, String realStr) {
    Answerer answerer = answererDao.queryAnswererByPhone(phone);
    if (answerer != null) {
      if (inputStr.equals(realStr)) {
        return true;
      } else {
        throw new RuntimeException("手机验证码不正确");
      }
    } else {
      throw new RuntimeException("该手机号下的租户不存在！");
    }
  }

  @Transactional
  @Override
  public boolean createAnswerer(Answerer answerer) {
    Answerer answerer1 = answererDao.queryAnswererByUsername(answerer.getUsername());
    if (answerer1 != null) {
      throw new RuntimeException("答者名已经存在");
    } else {
      int i = answererDao.insertAnswerer(answerer1);
      if (i == 1) {
        return true;
      } else {
        throw new RuntimeException("添加失败");
      }
    }
  }

  @Override
  public boolean insertAnswererToGroup(String groupname, String answerername) {
    Group group = groupDao.queryGroupByGroupname(groupname);
    Answerer answerer = answererDao.queryAnswererByUsername(answerername);
    GroupToAnswerer groupToAnswerer = groupToAnswererDao.queryRelation(group.getId(),
        answerer.getId());
    if (groupToAnswerer == null) {
      groupToAnswerer = new GroupToAnswerer();
      groupToAnswerer.setId(CommonUtils.getUUID())
          .setGroupId(group.getId())
          .setAnswererId(answerer.getId());
      int i = groupToAnswererDao.insertRelation(groupToAnswerer);
      if (i == 1){
        answerer.setRole(answerer.getRole()+1);
        updateAnswerer(answerer);
        return true;
      }else {
        throw new RuntimeException("插入失败！");
      }
    } else {
      throw new RuntimeException("该答者已经加入群组");
    }
  }
}
